package com.zs.toolbox.web2j.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtCommonPropertiesSetter;
import com.zs.toolbox.web2j.system.core.entity.WtUserMessageEntity;
import com.zs.toolbox.web2j.system.core.mapper.user.WtUserMessageMapper;
import com.zs.toolbox.web2j.system.core.request.WtBatchPrimaryKeyRequest;
import com.zs.toolbox.web2j.system.core.request.WtSaveUserMessageRequest;
import com.zs.toolbox.web2j.system.core.service.WtUserMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 用户-通知接口
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-10
 */
@Service
public class WtUserMessageServiceImpl extends ServiceImpl<WtUserMessageMapper, WtUserMessageEntity> implements WtUserMessageService {

    @Override
    public void saveMessages(WtSaveUserMessageRequest request) {

        WtUserMessageEntity wtUserMessageEntity = new WtUserMessageEntity();
        WtCommonPropertiesSetter.setDefaultCommonProperty(wtUserMessageEntity, WtAuthContext.getUserId());

        wtUserMessageEntity.setReaded(false).
                setMessageTitle(request.getMessageTitle()).
                setMessageContent(request.getMessageContent()).
                setMessageType(request.getMessageType());

        List<WtUserMessageEntity> wtUserMessageEntities = request.getReceiveUserId().stream().flatMap(uid -> {
            try {
                WtUserMessageEntity saveEntity = new WtUserMessageEntity();
                BeanUtils.copyProperties(wtUserMessageEntity, saveEntity);
                saveEntity.setUserId(uid);
                return Stream.of(saveEntity);
            } catch (Exception e) {
            }
            return Stream.empty();
        }).collect(Collectors.toList());
        this.saveBatch(wtUserMessageEntities);
    }

    @Override
    public WtUserMessageEntity getDetailAndSetReaded(WtPrimaryKeyReqeust<Integer> reqeust) {
        WtUserMessageEntity wtUserMessageEntity = this.getById(reqeust.getId());
        if (wtUserMessageEntity == null || wtUserMessageEntity.getUserId().equals(reqeust.getId())) {
            throw WtExceptions.E_USER_NO_PERMISSION;
        }

        if (!wtUserMessageEntity.getReaded()) {
            this.updateById(new WtUserMessageEntity().setId(wtUserMessageEntity.getId()).setReaded(true));
        }

        return wtUserMessageEntity;
    }

    @Override
    @Transactional
    public void setRead(WtBatchPrimaryKeyRequest<Integer> reqeust) {

        List<WtUserMessageEntity> list = this.list(new LambdaQueryWrapper<WtUserMessageEntity>()
                .in(WtUserMessageEntity::getId, reqeust.getIds())
                .eq(WtUserMessageEntity::getUserId, WtAuthContext.getUserId())
                .eq(WtUserMessageEntity::getReaded, false)
                .select(WtUserMessageEntity::getId)
        );

        List<WtUserMessageEntity> updateEntitys = list.stream().map(entity ->
                entity.setReaded(true))
                .collect(Collectors.toList());

        this.updateBatchById(updateEntitys);
    }
}
